package com.example.xaop.proxy;

import org.apache.catalina.User;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author xingyulai
 * @date 2024/3/8 3:55 PM
 * @description
 **/
public class ProxyMain {


    public static void main(String[] args) {

        UserInvocationHandler userInvocationHandler = new UserInvocationHandler();
        UserDAO userDAO = (UserDAO) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{UserDAO.class}, userInvocationHandler);
//        System.out.println(userDAO.getById("wenming"));

        OuterInvocationHandler outerInvocationHandler = new OuterInvocationHandler(userDAO);

        UserDAO outUserDAO = (UserDAO) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{UserDAO.class}, outerInvocationHandler);

        System.out.println(outUserDAO.getById("wenming"));
    }


}

class UserInvocationHandler implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        return 123L;
    }
}

class OuterInvocationHandler implements InvocationHandler {

    private Object target;

    public OuterInvocationHandler(Object target){
        this.target=target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object invoke = method.invoke(target, args);
        System.out.println("被代理的值=" + invoke);
        return 345L;
    }
}
